package com.ywq.app.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ywq.app.dao.AuxContentDao;
import com.ywq.app.domain.ArtReminder;
import com.ywq.app.domain.Channel;
import com.ywq.app.domain.Content;
import com.ywq.common.exception.AppBusinessException;


public class AuxContentDaoImpl extends SqlSessionDaoSupport implements AuxContentDao{
	
	private static final Logger logger = LoggerFactory.getLogger(AuxContentDaoImpl.class);

	@Override
	public int getIndexListCountByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getIndexListCountByChannel",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getRoomIndexListCount user failure.", e);
			}
			throw new AppBusinessException("getRoomIndexListCount user failure.", e);
		}
	}
	@Override
	public String getPartentByMark(String mark) throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne( "com.ywq.app.persistence.AuxContentMapper.getPartentByMark", mark);
			return (String) (obj == null ? "": obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getPartentByMark user failure.", e);
			}
			throw new AppBusinessException("getPartentByMark user failure.", e);
		}
	}
	

	@Override
	public List<Channel> getIndexListByChannel(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AuxContentMapper.getIndexListByChannel",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getRoomIndexList list failure.", e);
			}
			throw new AppBusinessException("getRoomIndexList list failure.", e);
		}
	}
	@Override
	public void insertChannel(Channel channel) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AuxContentMapper.insertChannel",
							channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertChannel user failure.", e);
			}
			throw new AppBusinessException("insertChannel user failure.", e);
		}
	}
	@Override
	public void insertContent(Content content) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.AuxContentMapper.insertContent",
							content);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertContent user failure.", e);
			}
			throw new AppBusinessException("insertContent user failure.", e);
		}
	}
	@Override
	public void updateContent(Content content) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AuxContentMapper.updateContent",
							content);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateContent user failure.", e);
			}
			throw new AppBusinessException("updateContent user failure.", e);
		}
	}
	//更新邮箱
	@Override
	public void updateArtReminder(ArtReminder artReminder) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AuxContentMapper.updateArtReminder",
							artReminder);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateArtReminder user failure.", e);
			}
			throw new AppBusinessException("updateArtReminder user failure.", e);
		}
	}
	
	//
	@Override
	public Channel getChannelById(String id) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getChannelById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById list failure.", e);
			}
			throw new AppBusinessException("getChannelById list failure.", e);
		}
	}
	/**
	 * 0728
	 */
	@Override
	public Channel getChannelByMark(String mark) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getChannelByMark",
							mark);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById list failure.", e);
			}
			throw new AppBusinessException("getChannelById list failure.", e);
		}
	}
	//
	@Override
	public void updateChannel(Channel channel) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AuxContentMapper.updateChannel",
							channel);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateChannel user failure.", e);
			}
			throw new AppBusinessException("updateChannel user failure.", e);
		}
	}
	/**
	 * 删除频道
	 */
	public void deleteChannel(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AuxContentMapper.deleteChannel",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateChannel user failure.", e);
			}
			throw new AppBusinessException("updateChannel user failure.", e);
		}
	}
	public void deleteContent(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.AuxContentMapper.deleteContent",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateChannel user failure.", e);
			}
			throw new AppBusinessException("updateChannel user failure.", e);
		}
	}
	
	/**
	 * 内容列表bychannel
	 * @param map
	 * @return
	 * @throws AppBusinessException
	 */
	@Override
	public List<Content> getChannelContentListById(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AuxContentMapper.getChannelContentListById",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelContentListById list failure.", e);
			}
			throw new AppBusinessException("getChannelContentListById list failure.", e);
		}
	}
	
	@Override
	public int getChannelContentListCountById(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getChannelContentListCountById",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelContentListCountById user failure.", e);
			}
			throw new AppBusinessException("getChannelContentListCountById user failure.", e);
		}
	}
	
	@Override
	public Content getStoryByArtistId(Map<String, Object> map) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getStoryByArtistId",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById list failure.", e);
			}
			throw new AppBusinessException("getChannelById list failure.", e);
		}
	}
	@Override
	public List<ArtReminder> getArtEmailList(Map<String, Object> map) throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"com.ywq.app.persistence.AuxContentMapper.getArtEmailList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtEmailList list failure.", e);
			}
			throw new AppBusinessException("getArtEmailList list failure.", e);
		}
	}
	@Override
	public int getArtEmailCount(Map<String, Object> map) throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.AuxContentMapper.getArtEmailCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtEmailCount user failure.", e);
			}
			throw new AppBusinessException("getArtEmailCount user failure.", e);
		}
	}
	
	
}
